package com.tevintao.marketingsystem.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.tevintao.marketingsystem.tool.PackageTool;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import java.util.HashMap;
import java.util.Map;


public abstract class SetTaskExtra {

    private Map<String, Object> mExtraMap = new HashMap<>();

    public boolean setExtra(Map<String, Object> map) {
        mExtraMap = map;
        return true;
    }

    public Object getExtra(String key) {
        if (WxTaskProcess.DEBUG_MODEL) {
            if (mExtraMap.containsKey(key)) {
                return mExtraMap.get(key);
            } else {
                return null;
            }
        } else {
            return WxTaskManger.getInstance().getExtra(key);
        }
    }


    protected boolean taskError(Context context, int state, String message) {
        if (WxTaskProcess.DEBUG_MODEL) {
            Intent i = new Intent(WxTaskManger.ACTION_TASK_ERROR);
            Bundle bundle = new Bundle();
            bundle.putString(WxTaskManger.KEY_TASK_ERROR_PACKAGE_NAME, PackageTool.getProcessName(context));
            bundle.putInt(WxTaskManger.KEY_TASK_ERROR_STATE, state);
            bundle.putString(WxTaskManger.KEY_TASK_ERROR_MESSAGE, message);
            i.putExtras(bundle);
            context.sendBroadcast(i);
        } else {
            WxTaskManger.getInstance().taskError(state, message);
        }
        return false;
    }

    public abstract boolean processView(String contextName, View v);
}
